<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Checked Arithmetic</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="library_implementation.html" title="Library Implementation">
<link rel="prev" href="checked_result.html" title="checked_result&lt;R&gt;">
<link rel="next" href="interval.html" title="interval&lt;R&gt;">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img href="index.html" height="164px" src="pre-boost.jpg" alt="Library Documentation Index"></td>
<td><h2>Safe Numerics</h2></td>
</tr></table>
<div class="spirit-nav">
<a accesskey="p" href="checked_result.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="library_implementation.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="interval.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.checked_arithmetic"></a>Checked Arithmetic</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="checked_arithmetic.html#idm462683643008">Description</a></span></dt>
<dt><span class="section"><a href="checked_arithmetic.html#idm462683641680">Type requirements</a></span></dt>
<dt><span class="section"><a href="checked_arithmetic.html#idm462683639680">Complexity</a></span></dt>
<dt><span class="section"><a href="checked_arithmetic.html#idm462683638528">Example of use</a></span></dt>
<dt><span class="section"><a href="checked_arithmetic.html#idm462683621264">Notes</a></span></dt>
<dt><span class="section"><a href="checked_arithmetic.html#idm462683619696">Synopsis</a></span></dt>
<dt><span class="section"><a href="checked_arithmetic.html#idm462683391280">See Also</a></span></dt>
<dt><span class="section"><a href="checked_arithmetic.html#idm462683389552">Header</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm462683643008"></a>Description</h4></div></div></div>
<p>Perform binary operations on arithmetic types. Return either a valid
    result or an error code. Under no circumstances should an incorrect result
    be returned.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm462683641680"></a>Type requirements</h4></div></div></div>
<p>All template parameters of the functions must model <a class="link" href="">Numeric</a> type requirements.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm462683639680"></a>Complexity</h4></div></div></div>
<p>Each function performs one and only one arithmetic operation.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm462683638528"></a>Example of use</h4></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">numeric</span><span class="special">/</span><span class="identifier">safe_numerics</span><span class="special">/</span><span class="identifier">checked_default</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">r</span> <span class="special">=</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">multiply</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span><span class="special">(</span><span class="number">24</span><span class="special">,</span> <span class="number">42</span><span class="special">)</span><span class="special">;</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm462683621264"></a>Notes</h4></div></div></div>
<p>Some compilers have command line switches (e.g. -ftrapv) which
    enable special behavior such that erroneous integer operations are
    detected at run time. The library has been implemented in such a way that
    these facilities are not used. It's possible they might be helpful in
    particular environment. These could be be exploited by re-implementing
    some functions in this library.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm462683619696"></a>Synopsis</h4></div></div></div>
<pre class="programlisting"><span class="comment">// safe casting on primitive types</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">cast</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// safe addition on primitive types</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">add</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// safe subtraction on primitive types</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">subtract</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// safe multiplication on primitive types</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">multiply</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// safe division on primitive types</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">divide</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// safe modulus on primitive types</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">modulus</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// safe less than predicate on primitive types</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">less_than</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// safe greater_than_equal predicate on primitive types</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">greater_than_equal</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// safe greater_than predicate on primitive types</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">greater_than</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// safe less_than_equal predicate on primitive types</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">less_than_equal</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// safe equal predicate on primitive types</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">equal</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// left shift</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">left_shift</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// right shift</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">right_shift</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// bitwise operations</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">bitwise_or</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">bitwise_and</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">&gt;</span>
<span class="identifier">checked_result</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">bitwise_xor</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&amp;</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm462683391280"></a>See Also</h4></div></div></div>
<p><a class="link" href="checked_result.html" title="checked_result&lt;R&gt;">checked_result&lt;R&gt;</a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm462683389552"></a>Header</h4></div></div></div>
<p><a href="../../include/boost/safe_numerics/checked_default.hpp" target="_top"><code class="computeroutput">#include
    &lt;boost/numeric/safe_numerics/checked_default.hpp&gt;
    </code></a></p>
<p><a href="../../include/boost/safe_numerics/checked_integer.hpp" target="_top"><code class="computeroutput">#include
    &lt;boost/numeric/safe_numerics/checked_integer.hpp&gt;
    </code></a></p>
<p><a href="../../include/checked.hpp" target="_top"><code class="computeroutput"></code></a><a href="../../include/boost/safe_numerics/checked_float.hpp" target="_top"><code class="computeroutput">#include
    &lt;boost/numeric/safe_numerics/checked_float.hpp&gt;
    </code></a></p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2012-2018 Robert Ramey<p><a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">Subject to Boost
      Software License</a></p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="checked_result.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="library_implementation.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="interval.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>
